package vn.gq.udv.utils;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.ProtocolException;
import java.net.URL;
import java.util.zip.GZIPInputStream;

import org.apache.http.HttpStatus;

import android.app.ProgressDialog;
import android.content.Context;
import android.util.Log;

public class SimpleDownload extends AsyncTask<String, Integer, Void>{

	public static final int PROGRESS_DIALOG_STYLE = 0;
	
	private String downloadPath;
	private DataType dataType = DataType.NORMAL;
	private ProgressDialog mProgressDialog;
	private OnDownloadCompleteListener downloadListener;
	
	public enum DataType {
		GZIP, NORMAL
	}
	
	public SimpleDownload(Context context) {
		mProgressDialog = new ProgressDialog(context);
		mProgressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
		mProgressDialog.setCancelable(false);
		mProgressDialog.setMessage("Downloading...");
	}
	
	public void download(String url, String path, OnDownloadCompleteListener listener) {
		download(url, path, DataType.NORMAL, listener);
	}
	
	public void download(String url, String path, DataType dataType, OnDownloadCompleteListener listener) {
		downloadPath = path;
		this.dataType = dataType;
		downloadListener = listener;
		execute(url);
	}
	
	@Override
	protected void onPreExecute() {
		super.onPreExecute();
		
		if (mProgressDialog != null) {
			mProgressDialog.show();
		}
	}

	@Override
	protected Void doInBackground(String... params) {
		
		String url = params[0];
		HttpURLConnection connection;
		URL uri;
		
		try {
			uri = new URL(url);
			InputStream is = null;
			connection = (HttpURLConnection) uri.openConnection();
			connection.setRequestMethod("GET");
			connection.connect();
			final int contentLengh = connection.getContentLength();
			Log.i("longhoanggiang", "--------contentLength: "+contentLengh);
			int code = connection.getResponseCode();
			if (code == HttpStatus.SC_OK) {
				is = connection.getInputStream();
				switch(dataType) {
					case GZIP:
						is = new GZIPInputStream(is);
						break;
					default:
						break;
				}
				
				IOUtils.copy(is, downloadPath, new IProgress() {
					
					@Override
					public void onPostProgress(int value) {
						if (contentLengh > 0) {
							publishProgress((int) value * 100 / contentLengh); 
						} else {
							publishProgress(value);
						}
					}
				});
			}
			connection.disconnect();
			
		} catch (MalformedURLException e) {
			e.printStackTrace();
		} catch (ProtocolException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}

	@Override
	protected void onPostExecute(Void result) {
		super.onPostExecute(result);
		if (mProgressDialog != null) {
			mProgressDialog.dismiss();
		}
		if (downloadListener != null) {
			downloadListener.onComplete(downloadPath);
		}
	}

	@Override
	protected void onProgressUpdate(Integer... values) {
		
		if (mProgressDialog != null) {
			mProgressDialog.setProgress(values[0]);
		}
	}
	
	public abstract class OnDownloadCompleteListener {
		public abstract void onComplete(String path);
	}

}
